﻿using System;
using System.Web.Services;
using System.Text.RegularExpressions;

using log4net;
using log4net.Config;

namespace MOReceive
{
    [WebService(Namespace = "http://linkcard.vn")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class MOReceive : System.Web.Services.WebService
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(MOReceive).Name);

        /// <summary>
        /// MO receiver
        /// </summary>
        /// <param name="MOId"></param>
        /// <param name="Telco"></param>
        /// <param name="ServiceNum"></param>
        /// <param name="Phone"></param>
        /// <param name="Syntax"></param>
        /// <param name="MessageRequest"></param>
        /// <param name="MessageResponse"></param>
        /// <returns></returns>
        [WebMethod]
        public int MOReceiver(int MOId, string Telco, string ServiceNum, string Phone, string Syntax, string MessageRequest, ref string MessageResponse)
        {
            // mặc định = 0: bản tin text
            // 1000: bản tin wappush
            int MessageCode = 0;
            // nội dung tin MT trả về
            MessageResponse = "Tin nhan thanh cong.";

            if (log.IsDebugEnabled)
            {
                log.Debug("MOID: " + MOId + ", Telco: " + Telco + ", ServiceNum: " + ServiceNum + ", Phone: " + Phone + ", Syntax: " + Syntax + ", MessageRequest: " + MessageRequest);
            }

            if (Syntax != "LC") // chỉ chấp nhận bản tin Linkcard, đầu số 8600
            {
                MessageCode = 1;
                MessageResponse = "Sai cu phap tin nhan. Xin moi ban thu lai.";
                return MessageCode;
            }

            try
            {
                // insert vào bảng mobile
                DateTime now = DateTime.Now;

                DBMobileActive mobile = new DBMobileActive();
                MobileActive mActive = new MobileActive();
                mActive.MOID = MOId;
                mActive.Telco = Telco;
                mActive.ServiceNum = ServiceNum;
                mActive.PhoneNumber = Phone;
                mActive.Syntax = Syntax;
                mActive.MessageRequest = MessageRequest;
                mActive.CreatedDate = now;
                mActive.CreateDateInt = now.Ticks;
                mobile.Insert(mActive);

                Regex regx = new Regex(@"LC\s(\w+)\s(\w+)"); // regular cho mã gửi lên
                Match match = regx.Match(MessageRequest);

                if (match.Success)
                {
                    string keyword = match.Groups[1].Value;
                    string content = match.Groups[2].Value;

                    switch (keyword)
                    {
                        case "KH": // tin nhắn kích hoạt
                            if (ActiveCustomer(content))
                            {
                                MessageResponse = "Ban da kich hoat thanh cong the Linkcard. Xin chao mung ban den voi dich vu tieu dung thong minh.";
                            }
                            else
                            {
                                MessageCode = 1;
                                MessageResponse = "Kich hoat bi loi. Xin moi ban thu lai. Hoac lien he voi chung toi qua trung tam ho tro: 19004600. Xin cam on.";
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageCode = 1;
                MessageResponse = "Kich hoat bi loi. Xin moi ban thu lai. Hoac lien he voi chung toi qua trung tam ho tro: 19004600. Xin cam on.";
                
                log.Error(ex.Message + " " + ex.StackTrace);
            }

            return MessageCode;
        }

        private bool ActiveCustomer(string cardNumber)
        {
            bool ret = false;

            DBCustomer cus = new DBCustomer();
            ret = cus.ActiveCustomer(cardNumber);

            return ret;
        }
    }

}
